{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "# 进程间通信\n",
    "在一个标准的操作系统中(例如在树莓派上使用的linux系统)，提供如下几种进程间通信方式：\n",
    "\n",
    "(1)信号。发送的信息量极少。就一个字。\n",
    "\n",
    "(2)管道(匿名管道)。可以发送一串信息了，但只能用于父子进程间通信。\n",
    "\n",
    "(3)命名管道。功能与管道一致，但可用于任何进程间通信。\n",
    "\n",
    "(4)消息队列。用于任何进程间通信，发送的消息由“自定义消息类型+消息数据”；优势：可以从消息队列中取出指定“自定义类型”的消息。\n",
    "\n",
    "(5)共享内存。用于任何进程间通信，多个进程都可以读取该内存区域；比消息队列的优势就是：快；缺点：需要在访问共享内存是通过锁的机制来保护，防止多个进程同时写内存，导致该内存区域数据异常。例如，进程1写该内存写了一半，内核调度进程2写该内存，进程2写完整段内存后，内核调度进程1继续写未完成的另一半，此时共享内存中的数据就是一半进程1写的，一半进程2写的，这不就错乱了。对于这段内存，我们称之为“临界资源”，下一章讲解临界资源保护的问题。\n",
    "\n",
    "(6)套接字(socket)。socket优点：就在于可以用于本计算机内任何进程通信，也可用于同在一个网络内的任意计算机上的进程间通信，可移植性好；缺点：发送的数据要至少要走一遍网络协议栈，所以会慢点。\n",
    "\n",
    "之前做过的一个项目就是就使用了套接字作为进程间通信手段，\n",
    "\n",
    "进程1:nodejs服务进程，用于接收远端浏览器发送的请求，并发送个请求处理进程；\n",
    "\n",
    "进程2：请求处理进程，通过套接字接收到请求后，控制本地的继电器进行动作。\n",
    "\n",
    "这样做的优势在于：nodejs服务器开发人员与请求处理进程开发人员分开进行，在各自的PC机上进行调试和自测试和两个程序的联调；联调完毕后，将套接字绑定的IP更改为127网段的即可；是不是极大地提高的开发效率。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Multiprocessing模块提供的进程间通信机制\n",
    "multiprocessing支持进程之间的两种通信通道：队列和管道。\n",
    "\n",
    "(1)mutliprocess的queues模块基本是在python的queue模块进行封装，可以配置成先入先出(FIFO)、后进先出(LIFO)、有优先级的队列(Priority Queue)；同时，mutliprocessing的queues是线程和进程安全的（不用加锁保护了）。\n",
    "\n",
    "(2)mutliprocess的Pipes模块，一个进程在管道一端发送数据、另一个进程在管道另一个端收数据，更能和队列类似，但没有后进先出、优先级等功能，同时最麻烦的是它不是线程和进程安全的，两个进程同时写或者同时读，需要加锁保护。(这里的锁就是下一章讲到的“进程同步机制”)\n",
    "\n",
    "后续的演示，使用mutliprocess的queues模块来进行。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on module multiprocessing.queues in multiprocessing:\n",
      "\n",
      "NAME\n",
      "    multiprocessing.queues\n",
      "\n",
      "MODULE REFERENCE\n",
      "    https://docs.python.org/3.7/library/multiprocessing.queues\n",
      "    \n",
      "    The following documentation is automatically generated from the Python\n",
      "    source files.  It may be incomplete, incorrect or include features that\n",
      "    are considered implementation detail and may vary between Python\n",
      "    implementations.  When in doubt, consult the module reference at the\n",
      "    location listed above.\n",
      "\n",
      "DESCRIPTION\n",
      "    # Module implementing queues\n",
      "    #\n",
      "    # multiprocessing/queues.py\n",
      "    #\n",
      "    # Copyright (c) 2006-2008, R Oudkerk\n",
      "    # Licensed to PSF under a Contributor Agreement.\n",
      "    #\n",
      "\n",
      "CLASSES\n",
      "    builtins.object\n",
      "        Queue\n",
      "            JoinableQueue\n",
      "        SimpleQueue\n",
      "    \n",
      "    class JoinableQueue(Queue)\n",
      "     |  JoinableQueue(maxsize=0, *, ctx)\n",
      "     |  \n",
      "     |  Method resolution order:\n",
      "     |      JoinableQueue\n",
      "     |      Queue\n",
      "     |      builtins.object\n",
      "     |  \n",
      "     |  Methods defined here:\n",
      "     |  \n",
      "     |  __getstate__(self)\n",
      "     |  \n",
      "     |  __init__(self, maxsize=0, *, ctx)\n",
      "     |      Initialize self.  See help(type(self)) for accurate signature.\n",
      "     |  \n",
      "     |  __setstate__(self, state)\n",
      "     |  \n",
      "     |  join(self)\n",
      "     |  \n",
      "     |  put(self, obj, block=True, timeout=None)\n",
      "     |  \n",
      "     |  task_done(self)\n",
      "     |  \n",
      "     |  ----------------------------------------------------------------------\n",
      "     |  Methods inherited from Queue:\n",
      "     |  \n",
      "     |  cancel_join_thread(self)\n",
      "     |  \n",
      "     |  close(self)\n",
      "     |  \n",
      "     |  empty(self)\n",
      "     |  \n",
      "     |  full(self)\n",
      "     |  \n",
      "     |  get(self, block=True, timeout=None)\n",
      "     |  \n",
      "     |  get_nowait(self)\n",
      "     |  \n",
      "     |  join_thread(self)\n",
      "     |  \n",
      "     |  put_nowait(self, obj)\n",
      "     |  \n",
      "     |  qsize(self)\n",
      "     |  \n",
      "     |  ----------------------------------------------------------------------\n",
      "     |  Data descriptors inherited from Queue:\n",
      "     |  \n",
      "     |  __dict__\n",
      "     |      dictionary for instance variables (if defined)\n",
      "     |  \n",
      "     |  __weakref__\n",
      "     |      list of weak references to the object (if defined)\n",
      "    \n",
      "    class Queue(builtins.object)\n",
      "     |  Queue(maxsize=0, *, ctx)\n",
      "     |  \n",
      "     |  Methods defined here:\n",
      "     |  \n",
      "     |  __getstate__(self)\n",
      "     |  \n",
      "     |  __init__(self, maxsize=0, *, ctx)\n",
      "     |      Initialize self.  See help(type(self)) for accurate signature.\n",
      "     |  \n",
      "     |  __setstate__(self, state)\n",
      "     |  \n",
      "     |  cancel_join_thread(self)\n",
      "     |  \n",
      "     |  close(self)\n",
      "     |  \n",
      "     |  empty(self)\n",
      "     |  \n",
      "     |  full(self)\n",
      "     |  \n",
      "     |  get(self, block=True, timeout=None)\n",
      "     |  \n",
      "     |  get_nowait(self)\n",
      "     |  \n",
      "     |  join_thread(self)\n",
      "     |  \n",
      "     |  put(self, obj, block=True, timeout=None)\n",
      "     |  \n",
      "     |  put_nowait(self, obj)\n",
      "     |  \n",
      "     |  qsize(self)\n",
      "     |  \n",
      "     |  ----------------------------------------------------------------------\n",
      "     |  Data descriptors defined here:\n",
      "     |  \n",
      "     |  __dict__\n",
      "     |      dictionary for instance variables (if defined)\n",
      "     |  \n",
      "     |  __weakref__\n",
      "     |      list of weak references to the object (if defined)\n",
      "    \n",
      "    class SimpleQueue(builtins.object)\n",
      "     |  SimpleQueue(*, ctx)\n",
      "     |  \n",
      "     |  Methods defined here:\n",
      "     |  \n",
      "     |  __getstate__(self)\n",
      "     |  \n",
      "     |  __init__(self, *, ctx)\n",
      "     |      Initialize self.  See help(type(self)) for accurate signature.\n",
      "     |  \n",
      "     |  __setstate__(self, state)\n",
      "     |  \n",
      "     |  empty(self)\n",
      "     |  \n",
      "     |  get(self)\n",
      "     |  \n",
      "     |  put(self, obj)\n",
      "     |  \n",
      "     |  ----------------------------------------------------------------------\n",
      "     |  Data descriptors defined here:\n",
      "     |  \n",
      "     |  __dict__\n",
      "     |      dictionary for instance variables (if defined)\n",
      "     |  \n",
      "     |  __weakref__\n",
      "     |      list of weak references to the object (if defined)\n",
      "\n",
      "DATA\n",
      "    __all__ = ['Queue', 'SimpleQueue', 'JoinableQueue']\n",
      "\n",
      "FILE\n",
      "    /usr/lib/python3.7/multiprocessing/queues.py\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "#更多关于multiprocessing.queues的介绍运行下面代码\n",
    "help(\"multiprocessing.queues\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
